package 从尾到头打印链表;

import java.util.LinkedList;

/**
 * 问题描述: 输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。
 */
public class SolutionV1 {

    private LinkedList<Integer> stack;

    public SolutionV1() {
        stack = new LinkedList<>();
    }

    public int[] reversePrint(ListNode head) {
        while(head != null) {
            stack.push(head.val);
            head = head.next;
        }
        int[] res = new int[stack.size()];
        for(int i = 0; i < res.length; i++) {
            res[i] = stack.pop();
        }
        return res;
    }
}
